#### LIBRARIES ####
library(foreign)
library(tidyverse)
library(stargazer)

#### FIG 2 ####

#### IMPORT DATA ####
cepf <- read.csv("Data/RD.csv")
cepf$pid45 <- .45
cepf$pid55 <- .55
cepf$pid35 <- .35
h <- 15*365

ggplot(cepf[cepf$dist<h,],aes(x=ym,y=pid))+
  geom_vline(xintercept=1970.75,color="gray70",linetype=2)+
  stat_smooth(data=cepf[cepf$elig==1&cepf$dist<h,],method = "lm",
              col = "black",se=F,aes(y=pid45))+
  stat_smooth(data=cepf[cepf$elig==0&cepf$dist<h,],method = "lm",
              col = "black",se=F,aes(y=pid45))+
  xlab("Date of Birth")+
  scale_y_continuous(limits=c(0,1))+
  ylab("Proportion Identifying with a Party")+
  ggtitle("Panel 1",subtitle="No Voting Effect, No Age Effect")+
  theme_bw()

ggsave("Figures/Fig2-1.pdf",height=4,width=4)

ggplot(cepf[cepf$dist<h,],aes(x=ym,y=pid))+
  geom_vline(xintercept=1970.75,color="gray70",linetype=2)+
  stat_smooth(data=cepf[cepf$elig==1&cepf$dist<h,],method = "lm",
              col = "black",se=F,aes(y=pid55))+
  stat_smooth(data=cepf[cepf$elig==0&cepf$dist<h,],method = "lm",
              col = "black",se=F,aes(y=pid35))+
  xlab("Date of Birth")+
  scale_y_continuous(limits=c(0,1))+
  ylab("Proportion Identifying with a Party")+
  ggtitle("Panel 2",subtitle="Positive Voting Effect, No Age Effect")+
  theme_bw()

ggsave("Figures/Fig2-2.pdf",height=4,width=4)


cepf$pid3 <- 13.2-cepf$ym/154

ggplot(cepf[cepf$dist<h,],aes(x=ym,y=pid))+
  geom_vline(xintercept=1970.75,color="gray70",linetype=2)+
  stat_smooth(data=cepf[cepf$elig==1&cepf$dist<h,],method = "lm",
              col = "black",se=F,aes(y=pid3))+
  stat_smooth(data=cepf[cepf$elig==0&cepf$dist<h,],method = "lm",
              col = "black",se=F,aes(y=pid3))+
  xlab("Date of Birth")+
  scale_y_continuous(limits=c(0,1))+
  ylab("Proportion Identifying with a Party")+
  ggtitle("Panel 3",subtitle="No Voting Effect, Positive Age Effect")+
  theme_bw()

ggsave("Figures/Fig2-3.pdf",height=4,width=4)

cepf$pid4 <- cepf$pid3-.07+.12*cepf$elig

ggplot(cepf[cepf$dist<h,],aes(x=ym,y=pid))+
  geom_vline(xintercept=1970.75,color="gray70",linetype=2)+
  stat_smooth(data=cepf[cepf$elig==1&cepf$dist<h,],method = "lm",
              col = "black",se=F,aes(y=pid4))+
  stat_smooth(data=cepf[cepf$elig==0&cepf$dist<h,],method = "lm",
              col = "black",se=F,aes(y=pid4))+
  xlab("Date of Birth")+
  scale_y_continuous(limits=c(0,1))+
  ylab("Proportion Identifying with a Party")+
  ggtitle("Panel 4",subtitle="Positive Voting Effect, Positive Age Effect")+
  theme_bw()

ggsave("Figures/Fig2-4.pdf",height=4,width=4)


#### FIG A.1 ####
partisan <- read.csv("Data/cep-pid.csv")

ggplot(partisan,aes(x=ym,y=1-none))+
  geom_point()+
  scale_y_continuous(limits=c(0,1),labels=scales::percent)+
  scale_x_continuous(breaks=seq(1990,2017,5),minor_breaks = seq(1990,2018,1))+
  ylab("Percent Identifying with a Political Party")+
  xlab("Year")+
  ggtitle("Partisanship in Chile")+
  theme_bw()

ggsave("Figures/FigA1.pdf",height=4,width=3.68)

#### FIG A.10 ####

cep82 <- data.frame(read.spss("Data/cep82.sav",use.value.labels=F))

cep82$edu <- cep82$DS_P4
cep82$edu[cep82$DS_P4>9] <- NA

cep82$pid <- as.numeric(cep82$MB_P12<77)
cep82$pid[cep82$MB_P12>77] <- NA

cep82$edux <- cep82$edu
cep82$edux[cep82$edu<4] <- 3
cep82$edux[cep82$edu>7] <- 8

cep82sum <- cep82%>%
  filter(!is.na(edux))%>%
  group_by(edux)%>%
  summarise(pid=mean(pid,na.rm=T))

cep82sum$edu2 <- c("Less Than Secondary","Complete Secondary",
                   "Incomplete Vocational","Complete Vocational",
                   "Incomplete University","Complete University")


ggplot(cep82sum,aes(y=pid,x = reorder(edu2, edux)))+
  geom_bar(stat="identity")+
  scale_y_continuous(limits=c(0,1))+
  ylab("Proportion Identifying with a Party")+
  xlab("Education Level")+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))

ggsave(file="Figures/FigA10.pdf",width=3.5,height=4)

#### TABLE A.1 ####

cep72 <- data.frame(read.spss("Data/cep72.sav",use.value.labels=F))

cep72$part <- recode(cep72$MB_P16,"88=NA;99=NA")
cep72$pid <- 1-as.numeric(cep72$part==9)
cep72$vote <- cep72$DS_P35
cep72$vote[cep72$vote==9] <- NA
cep72$vote[cep72$vote==2] <- 0

mv <- lm(vote~pid,data=cep72)
ml <- glm(vote~pid,data=cep72,family=binomial(link=logit))

stargazer(mv,ml, title="Partisanship and Turnout",digits=3,
          header=F,df=F,omit.stat=c("f","ser"),
          star.cutoffs=c(0.05,0.01,0.001),
          star.char=c("*","**","***"),
          notes="$^{*} p<0.05$; $^{**} p<0.01$; $^{***} p<0.001$",
          #      se = makerobustseslist(m1,m4,m5,m8),
          #      p = makerobustpslist(m1,m4,m5,m8),
          column.labels =c("OLS","Logit"), 
          dep.var.labels=c("Voted in 2013 Election"),
          covariate.labels=c("Party ID"),
          notes.append=F,table.placement="h!")



